
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Calendar;

import javax.sql.rowset.WebRowSet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;


public class Register extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String UserName;

	private JPanel contentPane;

	JTable table;
	DefaultTableModel model;
	private JTextField txtHoTen;
	private JTextField txtLop;
	private JTextField txtKhoa;
	private JTextField txtTongTien;
	private JTextField txtDiaDiem;

	private JComboBox cboKy, cboSubject;
	DefaultComboBoxModel modelKy, modelSubject;

	WebRowSet rs = null;
	DataProcess data = null;

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return this.UserName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.UserName = userName;
	}

	/**
	 * Create the frame.
	 */
	public Register(String studentid) {
		this.setUserName(studentid);
		try {
			try {
				data = (DataProcess) Naming.lookup("//khangutc/dkh");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}

		this.setTitle("Last login " + getLastLogin());
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 614, 476);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblMonHoc = new JLabel("Mon Hoc");
		lblMonHoc.setBounds(26, 140, 59, 20);
		contentPane.add(lblMonHoc);

		/**
		 * Combo Subject Settings
		 */
		modelSubject = new DefaultComboBoxModel();
		cboSubject = new JComboBox(modelSubject);
		fillCombo();
		cboSubject.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ComboListenner();
			}
		});
		cboSubject.setBounds(95, 140, 124, 20);
		contentPane.add(cboSubject);

		/**
		 * Table Settings
		 */
		String[] header = { "Ma Mon Hoc", "Ten Mon Hoc", "So DVHT", "Ngay DK" };
		model = new DefaultTableModel(header, 0);
		table = new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				cboSubject.setSelectedItem(table.getValueAt(
						table.getSelectedRow(), 1));
				ComboListenner();
				
				int i = (JOptionPane.showConfirmDialog(null,
						"Ban muon xoa khong?", "Thong bao",
						JOptionPane.YES_NO_OPTION,
						JOptionPane.QUESTION_MESSAGE));
				if (i == 0) {
					String MaSV = getUserName();
					String MaMH = String.valueOf(table.getValueAt(
							table.getSelectedRow(), 0));
					String MaLop = getMaLop();
					String sql = "Delete from Student_Register where (StudentID='"
							+ MaSV
							+ "') and (SubjectID='"
							+ MaMH
							+ "'"
							+ ") and (ClassID='" + MaLop + "')";
					try {
						data.ExecUpdateSQL(sql);
						updateRow();
						CalculateMoney();
					} catch (RemoteException ex) {
						ex.printStackTrace();
					}
				}
			}
		});
		
		fillTable();

		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(11, 215, 577, 148);
		contentPane.add(scrollPane);

		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				insertData();
				JOptionPane.showMessageDialog(null, "Insert successed!");
				CalculateMoney();
				updateRow();
			}
		});
		btnOk.setBounds(229, 139, 89, 23);
		contentPane.add(btnOk);

		JButton btnHuy = new JButton("Huy");
		btnHuy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CalculateMoney();
				updateRow();
			}
		});
		btnHuy.setBounds(493, 406, 89, 23);
		contentPane.add(btnHuy);

		JLabel lblCacMonDa = new JLabel("Cac Mon Da DK");
		lblCacMonDa.setBounds(10, 181, 195, 23);
		contentPane.add(lblCacMonDa);

		JLabel lblTongTien = new JLabel("Tong Tien");
		lblTongTien.setBounds(333, 374, 75, 23);
		contentPane.add(lblTongTien);

		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Th\u00F4ng Tin Chung",
				TitledBorder.CENTER, TitledBorder.TOP, null, null));
		panel.setBounds(16, 11, 566, 121);
		contentPane.add(panel);
		panel.setLayout(null);

		JLabel lblHoVaTen = new JLabel("Ho va ten:");
		lblHoVaTen.setBounds(10, 27, 75, 23);
		panel.add(lblHoVaTen);

		txtHoTen = new JTextField();
		txtHoTen.setEditable(false);
		txtHoTen.setBounds(84, 28, 201, 20);
		panel.add(txtHoTen);
		txtHoTen.setColumns(10);
		txtHoTen.setText(getStudentName());

		JLabel khoa = new JLabel("Khoa");
		khoa.setBounds(295, 27, 46, 23);
		panel.add(khoa);

		txtKhoa = new JTextField();
		txtKhoa.setEditable(false);
		txtKhoa.setBounds(351, 28, 201, 20);
		panel.add(txtKhoa);
		txtKhoa.setColumns(10);
		txtKhoa.setText(String.valueOf(getCourse()));

		JLabel lblLop = new JLabel("Lop");
		lblLop.setBounds(20, 71, 46, 23);
		panel.add(lblLop);

		txtLop = new JTextField();
		txtLop.setEditable(false);
		txtLop.setBounds(84, 72, 201, 20);
		panel.add(txtLop);
		txtLop.setColumns(10);
		txtLop.setText(getClassName());

		JLabel lblKy = new JLabel("Ky");
		lblKy.setBounds(295, 71, 46, 23);
		panel.add(lblKy);

		/**
		 * Combo Ky Hoc
		 */
		modelKy = new DefaultComboBoxModel();
		int i;
		for (i = 1; i < 10; i++) {
			modelKy.addElement(i);
		}

		cboKy = new JComboBox(modelKy);
		cboKy.setBounds(351, 72, 201, 20);
		panel.add(cboKy);

		txtTongTien = new JTextField("0");
		txtTongTien.setFont(new Font("Tahoma", Font.PLAIN, 13));
		txtTongTien.setForeground(Color.RED);
		txtTongTien.setEditable(false);
		txtTongTien.setBounds(395, 375, 187, 20);
		contentPane.add(txtTongTien);
		txtTongTien.setColumns(10);
		CalculateMoney();

		JLabel lblMaLop = new JLabel("Dia Diem");
		lblMaLop.setBounds(333, 140, 59, 20);
		contentPane.add(lblMaLop);

		txtDiaDiem = new JTextField();
		txtDiaDiem.setEditable(false);
		txtDiaDiem.setBounds(399, 140, 164, 20);
		contentPane.add(txtDiaDiem);
		txtDiaDiem.setColumns(10);

		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Calendar date = Calendar.getInstance();
				String lastlogin = String.valueOf(date.get(Calendar.DATE))
						+ "/" + String.valueOf(date.get(Calendar.MONTH)) + "/"
						+ String.valueOf(date.get(Calendar.YEAR)) + " "
						+ String.valueOf(date.get(Calendar.HOUR)) + ":"
						+ String.valueOf(date.get(Calendar.MINUTE)) + ":"
						+ String.valueOf(date.get(Calendar.SECOND));

				String sql = "Update Login set LastLogin='" + lastlogin
						+ "' where UserName='" + getUserName() + "'";
				try {
					data.ExecUpdateSQL(sql);
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				LoginForm frm = new LoginForm();
				frm.setVisible(true);
				setVisible(false);;
			}
		});
		btnExit.setBounds(11, 406, 89, 23);
		contentPane.add(btnExit);
	}

	/**
	 * Fill combo
	 */
	protected void fillCombo() {
		String sql = "Subject";
		try {
			rs = data.GetTable(sql);
			while (rs.next()) {
				modelSubject.addElement(rs.getString(2));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Fill Table
	 */
	protected void fillTable() {
		String sql = "select a.SubjectID,b.SubjectName,b.UnitOfStudy,a.DateRegis from Student_Register as a,Subject as b "
				+ "	where (a.SubjectID=b.SubjectID) and (a.StudentID='"
				+ getUserName() + "')";
		try {
			rs = data.GetDataBySQL(sql);
			while (rs.next()) {
				String[] row = { rs.getString(1), rs.getString(2),
						String.valueOf(rs.getInt(3)), rs.getString(4) };
				model.addRow(row);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Delete All Row
	 */
	protected void DeleteAllRow() {
		while (model.getRowCount() > 0) {
			model.removeRow(0);
		}
	}

	/**
	 * Update Table khi insert thanh cong
	 */
	protected void updateRow() {
		DeleteAllRow();
		fillTable();
	}

	/**
	 * ComboBox Listenner
	 */
	protected void ComboListenner() {
		String sql = "Select a.Location,b.ClassID from Class as a, Subject as b "
				+ "	where (a.ClassID=b.ClassID) and (b.ClassID=(Select c.ClassID from Subject as c "
				+ "	where c.SubjectName='"
				+ cboSubject.getSelectedItem().toString() + "'))";
		try {
			rs = data.GetDataBySQL(sql);
			while (rs.next()) {
				txtDiaDiem.setText(rs.getString(1));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Register Action
	 */
	protected void insertData() {
		Calendar date = Calendar.getInstance();
		int Ky;
		String Date = "";
		String sql = "";
		String MaSV = getUserName();

		Ky = Integer.parseInt(cboKy.getSelectedItem().toString());
		Date = String.valueOf(date.get(Calendar.DATE)) + "/"
				+ String.valueOf(date.get(Calendar.MONTH)) + "/"
				+ String.valueOf(date.get(Calendar.YEAR));

		sql = "insert into Student_Register values('" + MaSV + "','"
				+ getMaMH() + "','" + getMaLop() + "'," + Ky + ",'" + Date
				+ "')";
		try {
			data.ExecUpdateSQL(sql);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Calculate Money
	 */
	protected void CalculateMoney() {
		double S = 0;
		String sql = "select a.SubjectID,b.UnitOfStudy from Student_Register as a,Subject as b "
				+ "	where (a.SubjectID=b.SubjectID) and (a.StudentID='"
				+ getUserName() + "')";
		try {
			rs = data.GetDataBySQL(sql);
			while (rs.next()) {
				S += rs.getInt(2);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		S = S * 110000;
		txtTongTien.setText(String.valueOf(S));
	}

	/**
	 * Get Last login
	 */
	protected String getLastLogin() {
		String lastlogin = "";
		String condition = "UserName='" + getUserName() + "'";
		try {
			rs = data.GetRecord(condition, "Login");
			while (rs.next()) {
				lastlogin = rs.getString("LastLogin");
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return lastlogin;
	}

	/**
	 * Get Ho Ten
	 */
	protected String getStudentName() {
		String studentName = "";
		String condition = "StudentID='" + getUserName() + "'";
		try {
			rs = data.GetRecord(condition, "Student");
			while (rs.next()) {
				studentName = rs.getString("StudentName");
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return studentName;
	}

	/**
	 * Get Khoa
	 */
	protected int getCourse() {
		int course = 0;
		String condition = "StudentID='" + getUserName() + "'";
		try {
			rs = data.GetRecord(condition, "Student");
			while (rs.next()) {
				course = rs.getInt("Course");
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return course;
	}

	/**
	 * Get Lop
	 */
	protected String getClassName() {
		String className = "";
		String condition = "StudentID='" + getUserName() + "'";
		try {
			rs = data.GetRecord(condition, "Student");
			while (rs.next()) {
				className = rs.getString("Descipline");
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return className;
	}

	/**
	 * Get MA Mon Hoc
	 */
	protected String getMaMH() {
		String sql = "Select SubjectID from Subject where SubjectName='"
				+ cboSubject.getSelectedItem().toString() + "'";
		String MaMH = "";
		try {
			rs = data.GetDataBySQL(sql);
			while (rs.next()) {
				MaMH = rs.getString(1);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MaMH;
	}

	/**
	 * Get Ten Lop
	 */
	protected String getMaLop() {
		String sql = "Select * from Class where Location='"
				+ txtDiaDiem.getText() + "'";
		String MaLop = "";
		try {
			rs = data.GetDataBySQL(sql);
			while (rs.next()) {
				MaLop = rs.getString("ClassID");
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return MaLop;
	}


}
